<?php

namespace app\api\controller;


use app\admin\model\City;
use app\api\model\Auction as AuctionModel;
use app\api\model\AuctionBidLog;
use app\common\controller\Api;
use fast\Random;
use think\Db;

/**
 * 拍卖接口
 */
class Auction extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];



    //拍卖列表
    public function lists()
    {
        try {
            $param = input();
            $auction_model = new AuctionModel();

            $list = $auction_model->lists($param);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$list);

    }

    //拍卖品详情
    public function info()
    {
        $auction_id = input('auction_id',0);

        $info = \app\api\model\Auction::get($auction_id);
        if ($this->auth->id){
            $order = \app\api\model\AuctionOrder::get(['auction_id'=>$auction_id,'pay_status'=>1,'user_id'=>$this->auth->id]);
            $info['is_pay'] = $order?1:0;
            $info['order_id'] = $order?$order->order_id:0;
            $bid_log = AuctionBidLog::where(['auction_id'=>$auction_id,'status'=>1,'is_winning'=>1])->find();
            $info['winning_bid_log'] = $bid_log;
        }
        \app\api\model\Auction::where(['auction_id'=>$auction_id])->setInc('browse_num');
        $info['bid_log_count'] = AuctionBidLog::where(['auction_id'=>$auction_id])->count();
        $info['order_count'] = \app\api\model\AuctionOrder::where(['auction_id'=>$auction_id,'pay_status'=>1])->count();
        $this->success('',$info);
    }

    //出价
    public function bid()
    {
        $auction_id = input('auction_id',0);
        $order_id = input('order_id',0);
        $price = input('price',0);
        if (empty($auction_id)||empty($order_id)){
            $this->error('缺少必传参数');
        }
        if (empty($price)){
            $this->error('请输入价格');
        }
        $bid_model = new AuctionBidLog();
        $order_no = Random::alnum();
        $data = [
            'user_id'=>$this->auth->id,
            'order_id'=>$order_id,
            'auction_id'=>$auction_id,
            'price'=>$price,
            'order_no'=>$order_no
        ];
        $max_price = 0;
        $bid_log_list = AuctionBidLog::where(['auction_id'=>$auction_id])->column('price');
        if (count($bid_log_list)>0){
            $max_price = max($bid_log_list);
        }
        if ($price>$max_price){
            $data['status'] = 1;
            \app\api\model\Auction::where(['auction_id'=>$auction_id])->update(['price'=>$price]);
        } else {
            \app\api\model\Auction::where(['auction_id'=>$auction_id])->update(['price'=>$max_price]);
        }
        $res = $bid_model->save($data);
        if ($res){
            \app\api\model\Auction::where(['auction_id'=>$auction_id])->setInc('bid_count');
            AuctionBidLog::where(['auction_id'=>$auction_id])->where('price','<',$price)->update(['status'=>0]);
            $this->success('出价成功');
        } else {
            $this->error('出价失败');
        }
    }


    //增加关注数
    /*public function follow()
    {
        $auction_id = input('auction_id',0);
        \app\api\model\Auction::where(['auction_id'=>$auction_id])->setInc('follow_num');
        $this->success('成功');
    }*/

    //出价记录
    public function bidlog()
    {
        $auction_id = input('auction_id',0);

        $list = AuctionBidLog::where(['auction_id'=>$auction_id])
            ->order('price desc')
            ->paginate(10, false, ['query' => request()->get()]);

        $this->success('',$list);
    }

    //根据经纬度获取所在地
    public function getCity()
    {
        $lat = input('lat','');
        $lng = input('lng','');
        $model = new \app\admin\model\Auction;

        $res = $model->getcity($lat,$lng);

        $this->success('',$res['data']);
    }

    //获取全国的市
    public function city()
    {
        /*$provincelist = Db::name('area')->where(['level'=>2])
            ->where('pinyin','<>','')
            ->field('id,name,first,code,level')
            ->order('first asc')->select();*/


        $list=['A',"B","C",'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
        $where=[];

        $city=[];
        foreach ($list as $k=>$v){
            $city[]=Db::name('area')->where('first',$v)
                ->where($where)
                ->where('level',2)
                ->field('id,name,first,code,level')->select();
        }
        $data['indexList']=$list;
        $data['itemArr']=$city;

        $this->success('', $data);
    }

    //热门城市
    public function hotcity()
    {
        $city = City::order('weigh desc')->field('id,name,areas_id')->select();
        $this->success('请求成功',$city);
    }
}